var o = {};

var category = new Function();
category.prototype = o;

o.showAdd=function(){
	$('<div id="add_cate"></div>').appendTo($('body'));;
	$("#add_cate").load(ROOT+'/category/category-showAdd.action');
	$.ligerDialog.open({target:$("#add_cate") , height: 250, width: 450,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var form=$('#addform');
        			var parentCate=$('#parentCate').val();
        			if(!parentCate){
        				alert("上级分类不能为空");
        				return;
        			}
        			var id=$("#test3").val();
        			var cateName=$('#cateName').val();
        			if(!cateName){
        				alert("分类名不能为空");
        				return;
        			}
        			
        			$.ajax( {
        		 		type : "POST",
        		 		url : "category-add.action",
        		 		data : form.serialize()+"&parent="+id,
        		 		success : function(msg) {
        					alert(msg.jsonResult);
        					dialog.close();
        					window.location.reload(false);
        		 		},
        		 		error:function (XMLHttpRequest, textStatus, errorThrown) {
        		 		    alert("出错了："+errorThrown);
        		 		}
        		 	});
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
}

o.addTreeItem = function() {
	var node = manager.getSelected();
	var text = $("#txtNode").val();
	if(!/^[\u4e00-\u9fa5A-Za-z0-9]+$/.test(text)){
		 $.ligerDialog.error('只能输入英文汉字数字!');
		 return;
	}
	var check = $('#chkNode').attr('checked');
	if (!node) {
		$.ligerDialog.confirm('确定在根分类下建立子分类?', function(yes)
		{
			if(!yes){
				return;
			}else{
				$.ajax( {
					type : "POST",
					url : "category-add.action",
					data : "cate.name=" + text+"&cate.showInNav="+check,
					success : function(msg) {
						alert(msg.jsonResult);
						window.location.reload(false);
					},
					error : function(XMLHttpRequest, textStatus, errorThrown) {
						alert("出错了：" + errorThrown);
					}
				});
			}
		});
	}else{
		$.ajax( {
			type : "POST",
			url : "category-add.action",
			data : "cate.name=" + text+"&cate.showInNav="+check+"&parent="+node.data.id,
			success : function(msg) {
				alert(msg.jsonResult);
				window.location.reload(false);
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert("出错了：" + errorThrown);
			}
		});
	}
}

o.showEdit=function(){
	var node = manager.getSelected();
	if (!node) {
		$.ligerDialog.error('请选择要修改的分类!');
		 return;
	}
	$('<div id="edit_cate"></div>').appendTo($('body'));;
	$("#edit_cate").load(ROOT+'/category/category-showEdit.action?id='+node.data.id);
	$.ligerDialog.open({target:$("#edit_cate") , height: 250, width: 450,modal:true,buttons: [
        { text: '确定', onclick: function (item, dialog) {
        			var form=$('#addform');
        			var parentCate=$('#parentCate').val();
        			if(!parentCate){
        				alert("上级分类不能为空");
        				return;
        			}
        			var id=$("#test3").val();
        			var cateName=$('#cateName').val();
        			if(!cateName){
        				alert("分类名不能为空");
        				return;
        			}
        			
        			$.ajax( {
        		 		type : "POST",
        		 		url : "category-edit.action",
        		 		data : form.serialize()+"&parent="+id+"&id="+node.data.id,
        		 		success : function(msg) {
        					alert(msg.jsonResult);
        					dialog.close();
        					window.location.reload(false);
        		 		},
        		 		error:function (XMLHttpRequest, textStatus, errorThrown) {
        		 		    alert("出错了："+errorThrown);
        		 		}
        		 	});
        		 	
        	} },
        { text: '取消', onclick: function (item, dialog) { dialog.close(); } }
     ]
     });
}

o.showDesc=function(){
	var node = manager.getSelected();
	if (!node) {
		$.ligerDialog.error('请选择一级分类!');
		 return;
	}
	if (!node.data.root) {
		$.ligerDialog.error('请选择一级分类!');
		 return;
	}
	window.open('category-showDesc.action?id='+node.data.id);
}

o.editTreeItem=function(){
	var node = manager.getSelected();
	var text = $("#txtNode").val();
	if(!/^[\u4e00-\u9fa5A-Za-z0-9]+$/.test(text)){
		 $.ligerDialog.error('只能输入英文汉字数字!');
		 return;
	}
	var check = $('#chkNode').attr('checked');
	if (!node) {
		$.ligerDialog.error('请选择要修改的分类!');
		 return;
	}
	$.ajax( {
		type : "POST",
		url : "category-edit.action",
		data : "cate.name=" + text+"&cate.showInNav="+check+"&cate.id="+node.data.id+"&cate.position="+node.data.position,
		success : function(msg) {
			alert(msg.jsonResult);
			window.location.reload(false);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			alert("出错了：" + errorThrown);
		}
	});
}

o.removeTreeItem=function(){
	var node = manager.getSelected();
	if (!node) {
		$.ligerDialog.error('请选择要修改的分类!');
		 return;
	}
	$.ligerDialog.confirm('确定删除该分类?', function(yes)
			{
				if(yes){
					var ids=node.data.id;
					$.ajax( {
						type : "POST",
						url : "category-delete.action",
						data : "ids=" + ids,
						success : function(msg) {
							alert(msg.jsonResult);
							window.location.reload(false);
						},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
							alert("出错了：" + errorThrown);
						}
					});
				}
			}
	);
	
}

o.changeItemPosition=function(){
	var id=$("#test3").val();
	if(!id){
		$.ligerDialog.error('请选择要移至哪个分类!');
		return;
	}
	var node = manager.getSelected();
	if(!node){
		$.ligerDialog.error('请选择要修改位置的分类!');
		return;
	}
	var type=$("#test4").val();
	$.ajax( {
		type : "POST",
		url : "category-changePos.action",
		data : "id=" + node.data.id+"&toId="+id+"&type="+type,
		success : function(msg) {
			alert(msg.jsonResult);
			window.location.reload(false);
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			alert("出错了：" + errorThrown);
		}
	});
}
